package com.vernhe.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.RemoteTokenServices;

/**
 * OAuth2资源服务器配置
 *
 * @author VernHe
 * @date 2022年02月26日 14:17
 */
@Configuration
public class Oauth2ResourceServerConfiguration extends ResourceServerConfigurerAdapter {

    /**
     * 认证服务器检查token的端点
     */
    private static final String CHECK_TOKEN_URL = "http://localhost:4444/oauth/check_token";

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        // 每次验证都需要找认证服务器，效率比较低
        RemoteTokenServices tokenServices = new RemoteTokenServices();

        tokenServices.setCheckTokenEndpointUrl(CHECK_TOKEN_URL);
        tokenServices.setClientId("cms");
        tokenServices.setClientSecret("secret");

        resources.tokenServices(tokenServices);
    }
}
